import {
	objDeepCopy,
	getDateString
} from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	created() {
		this.init();
	},
	mounted() {

	},
	methods: {
		init() {

			this.getCardNumber(this, undefined); //全部
			this.getCardNumber(this, 30); //
			this.getCardNumber(this, 40); //
			// 
			this.getAllocationList(this);
			// this.getinoutStatus(this);//改为写死（张鑫大佬要求，原本为聂素接口）
			this.getTypeSelect(this);
			this.getStoreSelect(this);
			// this.getCardNumber(this)//3.15删除
		},
		//获取列表接口()
		getAllocationList(that, params, page) {
			params = params || {
				businessType: 90
			}
			page = page || that.Model.page;
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				// url:'/api/whWarehouse/list',
				url: apiTest + '/supplier/whWarehouse/list',
				data: {
					condition: params,
					pageIndex: page.pageIndex,
					pageSize: page.pageSize,
				}
			}).then((res) => {
				console.log(res);
				that.Model.tableData = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result.result;
					var pagination = commonObject.paginationFunction(data.result);
					that.Model.paginationObject = objDeepCopy(pagination);
					if (result) {
						for (var i = 0; i < result.length; i++) {
							obj = objDeepCopy(result[i]);
							// 发货仓库
							if (result[i].outWarehouseInfo) {
								obj.outWarehouse = result[i].outWarehouseInfo.theNameOfThePlace + result[i].outWarehouseInfo.descriptionOfWarehouseLocation;
							}
							// 收货仓库
							if (result[i].inWarehouseInfo) {
								obj.inWarehouse = result[i].inWarehouseInfo.theNameOfThePlace + result[i].inWarehouseInfo.descriptionOfWarehouseLocation;
							}
							// 单据状态 
							if (result[i].statusDict) {
								obj.orderStatus = result[i].statusDict.value;
							}
							//出库单类型
							if (result[i].typeDict) {
								obj.type = result[i].typeDict.value;
							}
							that.Model.tableData.push(obj)
						}
					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//获取 出入库状态 下拉框
		getinoutStatus(that) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/base/systemDictionary/getByType/warehouseStatus',
				data: {

				}
			}).then((res) => {
				console.log(res);
				that.Model.slectStatusData = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					for (var i = 0; i < result.length; i++) {
						obj = {
							label: result[i].value,
							value: result[i].key
						}
						that.Model.slectStatusData.push(obj);
					}
					that.Model.filterOption.items[3].data = objDeepCopy(that.Model.slectStatusData);
				} else {

				}

			});
		},
		//获取 出入库类型 下拉框
		getTypeSelect(that) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/supplier/whAllot/getType',
			}).then((res) => {
				console.log(res);
				that.Model.slectTypeData = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					for (var i = 0; i < result.length; i++) {
						obj = {
							label: result[i].value,
							value: result[i].key
						}
						that.Model.slectTypeData.push(obj);
					}
					that.Model.filterOption.items[1].data = objDeepCopy(that.Model.slectTypeData);
				} else {

				}

			});
		},
		//获取 发货仓库 下拉框
		getStoreSelect(that) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/supplierFactoryLocation/listNoPage',
				data: {

				}
			}).then((res) => {
				console.log(res);
				that.Model.slectTypeData = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					for (var i = 0; i < result.length; i++) {
						var label = result[i].theNameOfThePlace + '-' + result[i].descriptionOfWarehouseLocation;
						obj = {
							label: label,
							value: result[i].id
						}
						that.Model.slectTypeData.push(obj);
					}
					that.Model.filterOption.items[2].data = objDeepCopy(that.Model.slectTypeData);
				} else {

				}

			});
		},
		// 获取 卡片数量
		getCardNumber(that, status) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/whWarehouse/list',
				data: {
					condition: {
						businessType: 90,
						status: status
					}
				}
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					var result = data.result;
					if (status == 30) {
						that.Model.configArr[1].count = data.result.total;
					} else if (status == 40) {
						that.Model.configArr[2].count = data.result.total;
					} else {
						that.Model.configArr[0].count = data.result.total;
					}
				} else {


				}

			});
		},
		//卡片切换 回调
		cardEmit(card) {
			var params = {
				businessType: 90,
				status: card.value
			}
			// var searchFromData = this.Model.searchFromData;
			this.Model.filterOption.items[3].value = card.label;
			this.getAllocationList(this, params);
		},
		//点击打开快递信息 弹窗
		trackingNo() {
			this.Model.tipsShow = false;
			var TableSelectData = this.Model.TableSelectData;
			var idArr = []; //id数组
			if (TableSelectData.length == 0) {
				this.tips(this, 'warning', '请先勾选需要填写物流单的数据');
				return;
			}
			// 判断 是否之前填写过物流单
			for (let i = 0; i < TableSelectData.length; i++) {
				if (TableSelectData[i].logisticsNumber) {
					this.Model.tipsShow = true;
				}
				idArr.push(TableSelectData[i].id);
			}
			this.Model.idArrary = objDeepCopy(idArr);
			//执行获取物流方法
			this.getLogisticsAddress(this, idArr);

		},
		//获取物流展示地址
		getLogisticsAddress(that, ids) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/whAllot/getLogisticsAddress',
				data: {
					ids: ids
				}
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					if (result) {
						that.Model.inMsg = result.inWarehouseInfo.theNameOfThePlace + result.inWarehouseInfo.descriptionOfWarehouseLocation; //收货仓
						that.Model.outMsg = result.outWarehouseInfo.theNameOfThePlace + result.outWarehouseInfo.descriptionOfWarehouseLocation; //发货仓
						that.Model.editDialogFlag = true;
					}
				} else if (data.status == 500) {
					if (data.code == '1000001') {
						that.tips(that, 'warning', '收发货地址不相同');
					} else {
						that.tips(that, 'warning', data.msg);
					}

				}

			});
		},
		//点击获取物流单号(顺丰)
		onGetShipmentNo() {
			if (this.Model.companyOptionVal == 'ems') {
				return;
			}
			this.Model.shipmentNumber = '';
			var that = this;
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/whAllot/getLogistics',
				data: {
					logisticsCompany: "shunfeng",
				}
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					if (result) {
						that.Model.shipmentNumber = result;
					}
				} else {

				}

			});
		},
		//点击保存 物流单
		saveLogistics() {
			var idArrary = this.Model.idArrary;
			var logisticsCompany = this.Model.companyOptionVal;
			var logisticsNumber = this.Model.shipmentNumber;

			var that = this;
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/whAllot/saveLogistics',
				data: {
					ids: idArrary,
					logisticsCompany: logisticsCompany,
					logisticsNumber: logisticsNumber
				}
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					that.tips(that, 'success', '操作成功');
					setTimeout(function() {
						that.getAllocationList(that);
						that.Model.editDialogFlag = false;
					}, 1000);
				} else {

				}

			});
		},

		//切换选项
		optionChange(val) {
			this.Model.shipmentNumber = '';
			if (val == 'shunfeng') {
				this.Model.isDisable = true;
			} else {
				this.Model.isDisable = false;
			}
		},
		//点击
		storageOperating(row) {
			this.$router.push({
				path: 'outsideAllStoreOut',
				query: {
					id: row.id,
					number: row.number
				}
			});
		},
		//跳转到详情页
		toDetial(row) {
			this.$router.push({
				path: 'outsideAllStoreDetail',
				query: {
					number: row.number
				}
			});
		},
		//点击新建
		newPage(row) {
			this.$router.push({
				path: 'outsideAllocationStoreNew',
				query: {
					id: row.id
				}
			});
		},
		//表格二次确认弹窗(作废或者出库)
		secConfirm(row, type) {
			if (type == 'out') {
				this.$confirm('此操作将出库, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
				}).then(() => {
					this.outAjax(this, row.id);
				}).catch(() => {

				});
			} else if (type == 'obsolete') {
				this.$confirm('此操作将作废, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
				}).then(() => {
					this.obAjax(this, row.id);
				}).catch(() => {

				});
			} else {

			}

		},
		//作废 的请求
		obAjax(that, id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/supplier/whAllot/cancle/' + id,
			}).then((res) => {
				console.log(res);
				that.Model.slectTypeData = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					that.tips(that, 'success', '操作成功');
					that.getAllocationList(that);
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//出库 的请求
		outAjax(that, id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/supplier/whAllot/outStorage?id=' + id,
				// url:'/api/whAllot/outStorage?id='+id,
			}).then((res) => {
				console.log(res);
				that.Model.slectTypeData = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					that.tips(that, 'success', '操作成功');
					that.getAllocationList(that);
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {
			var searchFromData = this.Model.searchFromData;
			this.Model.page.pageIndex = val;
			this.getAllocationList(this, searchFromData, this.Model.page);
		},
		handleSizeChange(val) {
			var searchFromData = this.Model.searchFromData;
			this.Model.page.pageSize = val;
			this.getAllocationList(this, searchFromData, this.Model.page);
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = {
				businessType: 90,
				keyWord: val.keyWord,
				logisticsCompany: val.logisticsCompany,
				status: val.status,
				type: val.type,
				inWarehouseId: val.inWarehouseId
			}
			if (val.outSureTime) {
				this.Model.searchFromData.outSureTimeStart = getDateString(val.outSureTime[0]);
				this.Model.searchFromData.outSureTimeEnd = getDateString(val.outSureTime[1]);
			}
		},
		toSearch(val) {
			var searchFromData = this.Model.searchFromData;
			this.getAllocationList(this, searchFromData);
		},
		toClear(val) {
			this.Model.searchFromData = {};
			this.Model.filterOption.items[1].data = objDeepCopy(this.Model.slectTypeData);
			this.Model.filterOption.items[3].data = objDeepCopy(this.Model.slectStatusData);
			this.Model.filterOption.items[2].data = objDeepCopy(this.Model.slectTypeData);
		},
		toExport(val) {
			console.log(val);
		},

		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
	},
	watch: {
		$route: {
			handler(newValue, oldValue) {
				this.init();
			},
			deep: true
		},

	},
	filters: {
		objFilters: function(value) {

		}
	}
}
export default Controller;
